<template>
	<div class="app-container">
		<!-- 查询和其他操作 -->
		<div class="filter-container">
			<el-input v-model="listQuery.sellerName" clearable style="width: 180px" class="filter-item"
			          placeholder="店铺名称"/>
			<el-select v-model="listQuery.companyType" style="width: 180px" class="filter-item" placeholder="请选择店铺类型">
				<el-option v-for="(key, value) in typeMap" :key="key" :label="key" :value="value"/>
			</el-select>
			<el-button v-permission="[1]" clearable class="filter-item" type="primary" icon="el-icon-search"
			           @click="handleFilter">查找
			</el-button>
		</div>

		<!-- 查询结果 -->
		<el-table v-loading="listLoading" :data="list" size="small" element-loading-text="正在查询中。。。" border fit
		          highlight-current-row>

			<el-table-column align="center" width="100" label="操作" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button v-permission="[1]" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑
					</el-button>
				</template>
			</el-table-column>
			<el-table-column align="center" width="80" label="店铺编号" prop="id"/>

			<el-table-column align="center" label="店铺名称" prop="sellerName"/>
			<el-table-column align="center" label="店铺类型" prop="companyType">
				<template slot-scope="scope">
					<el-tag>{{ scope.row.companyType | shopTypeFilter }}</el-tag>
				</template>
			</el-table-column>
			<el-table-column align="center" width="120" label="店铺logo" prop="logoPic">
				<template slot-scope="scope">
					<img v-if="scope.row.logoPic" :src="scope.row.logoPic" style="width: 100px; height: 100px">
				</template>
			</el-table-column>


			<el-table-column align="center" label="经营范围" prop="scope"/>
			<el-table-column align="center" label="法人" prop="legalPerson"/>
			<el-table-column align="center" label="公司地址" prop="address"/>
			<el-table-column align="center" label="公司电话" prop="telephone"/>
			<el-table-column align="center" label="创建日期" prop="createTime">
				<template slot-scope="scope">{{ scope.row.createTime | formatTime }}</template>
			</el-table-column>


		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
		            @pagination="getList"/>

	</div>
</template>

<script>
	import {listShops, updateShops} from '@/api/shops'
	import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

	const typeMap = {
		0: '采购商',
		1: '供应商'
	}

	export default {
		name: 'Order',
		components: {Pagination},
		filters: {
			shopTypeFilter(status) {
				return typeMap[status]
			}
		},
		data() {
			return {
				list: undefined,
				total: 0,
				listLoading: true,
				listQuery: {
					pageNum: 1,
					pageSize: 20,
					sellerName: undefined,
					companyType: undefined
				},
				typeMap
			}
		},
		created() {
			this.getList()
		},
		methods: {
			getList() {
				this.listLoading = true
				listShops(this.listQuery)
					.then(res => {
						this.list = res.data.records
						this.total = res.data.total
						this.listLoading = false
					})
					.catch(() => {
						this.list = []
						this.total = 0
						this.listLoading = false
					})
			},
			handleImgArray(arr) {
				return arr.split(',')
			},
			handleUpdate(row) {
				this.$store.dispatch('clearMerchant')
				this.$store.dispatch('setMerchant', row)
				this.$router.push({path: '/shops/merchant', query: {id: row.id}})
			},
			handleFilter() {
				this.listQuery.pageNum = 1
				this.getList()
			}
		}
	}
</script>
